package com.zmzncs.lmtc.module.order.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmzncs.lmtc.common.annotation.Idempotent;
import com.zmzncs.lmtc.common.pojo.PageVO;
import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.util.JsonUtil;
import com.zmzncs.lmtc.common.util.RestUtil;
import com.zmzncs.lmtc.common.util.WeChatUtil;
import com.zmzncs.lmtc.module.order.pojo.order_trade_record.PayBehalfForm;
import com.zmzncs.lmtc.module.order.pojo.order_trade_record.OrderTradeRecordDTO;
import com.zmzncs.lmtc.module.order.pojo.order_trade_record.OrderTradeRecordTableVO;
import com.zmzncs.lmtc.module.order.service.OrderTradeRecordService;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单-订单交易记录 前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2020-09-24
 */
@Api(tags = "订单交易记录")
@RestController
@RequestMapping("/order/info-trade-record")
@Validated
@Slf4j
public class OrderTradeRecordController {

    @Autowired
    OrderTradeRecordService orderTradeRecordService;


    @ApiOperation(value = "分页查询用户余额订单记录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name = "currentPage", value = "页码", required = true),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "每页记录数", required = true),
    })
    @PostMapping(value = "getOrderTradeRecordRecharge")
    public Rest<PageVO<OrderTradeRecordTableVO>> getOrderTradeRecordRecharge(@NotNull Integer currentPage, @NotNull Integer pageSize) {

        IPage<OrderTradeRecordDTO> iPage = orderTradeRecordService.getOrderTradeRecordRecharge(new Page(currentPage, pageSize));
        PageVO<OrderTradeRecordTableVO> recordTableVOPageVO = JsonUtil.pageTransform(iPage, OrderTradeRecordTableVO.class);

        return RestUtil.success(recordTableVOPageVO);
    }

    @ApiOperation(value = "分页查询垫付未支付订单记录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name = "currentPage", value = "页码", required = true),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "每页记录数", required = true),
    })
    @PostMapping(value = "getOrderTradeRecordBehalfPay")
    public Rest<PageVO<OrderTradeRecordDTO>> getOrderTradeRecordBehalfPay(@NotNull Integer currentPage, @NotNull Integer pageSize) {

        IPage<OrderTradeRecordDTO> iPage = orderTradeRecordService.getOrderTradeRecordBehalfPay(new Page(currentPage, pageSize));
        PageVO<OrderTradeRecordDTO> recordTableVOPageVO = JsonUtil.pageTransform(iPage, OrderTradeRecordDTO.class);

        return RestUtil.success(recordTableVOPageVO);
    }

    @ApiOperation(value = "支付垫付停车费")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "pay-behalf")
    @Idempotent
    public Rest behalfPay(@RequestBody PayBehalfForm form) {
        Map<String, Object> behalfPay = orderTradeRecordService.behalfPay(form);

        return RestUtil.success(behalfPay);
    }

    @ApiOperation(value = "微信支付回调-垫付")
    @PostMapping(value = "wechat/behalfPay/callback")
    public String behalfPayCallback(HttpServletRequest request) {
        String callbackStr = WeChatUtil.parseCallback(request);
        if (StringUtils.isBlank(callbackStr)) {
            return WeChatUtil.wxpayCallbackResult(false);
        }

        return orderTradeRecordService.behalfPayCallback(callbackStr);
    }

    @ApiOperation(value = "获取欠费订单")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @GetMapping(value = "/get-arrears-order")
    public Rest<List<OrderTradeRecordDTO>> getArrearsOrder(){
        List<OrderTradeRecordDTO> tradeRecordDTOList = orderTradeRecordService.getArrearsOrder();

        return RestUtil.success(tradeRecordDTOList);
    }

}

